//
//  YCDescViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/27.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  简介

#import "YCDescViewController.h"
#import "YCProfileDescModel.h"

@interface YCDescViewController ()<SDCycleScrollViewDelegate>

/** 文本标签  */
@property (nonatomic, weak) UILabel *textLabel;
/** 文本内容  */
@property (nonatomic, copy) NSString *text;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
/** 数据模型  */
@property (nonatomic, strong) YCProfileDescModel *descModel;
@end

@implementation YCDescViewController

#pragma mark - 数据处理

- (void)loadData
{
    NSDictionary *params = @{//暂时不用传参
//                             @"uid":@"1",
//                             @"token":accessToken_Test,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIAbout params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        YCLog(@"当前线程:%@",[NSThread currentThread]);
        self.descModel = [YCProfileDescModel yc_objectWithKeyValues:response.data];
        [self reloadData];
        [self.scrollView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.scrollView.mj_header endRefreshing];
    }];
}

- (void)loadDataByAfnOrigin
{
    NSDictionary *params = @{//暂时不用传参
                             //                             @"uid":@"1",
                             //                             @"token":accessToken_Test,
                             };
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    [mgr POST:[YCAPIAbout appendBaseUrl] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.descModel = [YCProfileDescModel yc_objectWithKeyValues:responseObject];
        [self reloadData];
        [self.scrollView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YCLog(@"%@",error.localizedDescription);
        [self.scrollView.mj_header endRefreshing];
    }];
}

- (void)reloadData
{
//    [self.descModel.imgs makeObjectsPerformSelector:@selector(appendBaseUrl)];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSString *str in self.descModel.imgs) {
        [arrayM addObject:[str appendBaseUrl]];
    }
    self.descModel.imgs = [NSArray arrayWithArray:arrayM];
    self.cycleScrollView.imageURLStringsGroup = self.descModel.imgs;
    self.text = self.descModel.describe;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self makeView];
    self.scrollView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
//        [self loadData];
        [self loadDataByAfnOrigin];
    }];
    self.text = @"云南一乘驾驶培训股份有限公司于2011年8月11日注册成立。2012年正式成为云南省城市建设投资集团有限公司下属企业。截止到2015年1月，项目总投资已超过9亿元，教练车数量突破1500辆。被云南省行业管理部门授予“一等驾校”等称号。未来发展目标，建成以驾驶培训考试为核心，以汽车运动中心、汽车主题公园、汽车文化体验及高端增值服务为一体的综合性、国际化、复合型驾培服务集团。一乘驾校将成为西南三省规模最大、功能最全、管理最优、服务最好、设施最先进、绿色环保、一站式、园林式一等（五星级）驾培机构，成为汽车产业文化的领跑者。一乘驾校电话：4000125111。一乘驾校地址：云南省昆明市晋宁县晋城镇石碑村。";
//    [self loadData];//暂时不调,接口返回不规范
    [self loadDataByAfnOrigin];//afn原生的,不用封装的
}

// 创建UI
- (void)makeView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView = scrollView;
    
    // uiscrollView布局,要一个中间容器view
    UIView *containView = [[UIView alloc] init];
    [self.scrollView addSubview:containView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];
    
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, 202) delegate:self placeholderImage:[UIImage yc_placeHolderImageForSize:CGSizeMake(kScreenWidth, 202)]];
    cycleScrollView.imageURLStringsGroup = @[@"http://img1.3lian.com/2015/w7/98/d/22.jpg",
                                               @"http://img1.3lian.com/2015/w7/90/d/5.jpg",
                                               @"http://img1.3lian.com/2015/w7/90/d/1.jpg",
                                               @"http://pic37.nipic.com/20140209/8821914_163234218136_2.jpg",
                                               @"http://pic39.nipic.com/20140226/18071023_162553457000_2.jpg",
                                               @"http://imgsrc.baidu.com/forum/pic/item/645b8701a18b87d6e716e197070828381e30fdae.jpg"];//网络图片链接数组
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.pageDotColor = [UIColor yc_colorWithUInt:0xffffff alpha:0.5];
    cycleScrollView.currentPageDotColor = [UIColor yc_colorWithUInt:0xffffff];
    cycleScrollView.pageControlDotSize = CGSizeMake(12, 12);
    cycleScrollView.hidesForSinglePage = YES;
    cycleScrollView.autoScrollTimeInterval = 2.0;//2s自动轮播
    [containView addSubview:cycleScrollView];
    self.cycleScrollView = cycleScrollView;
    [cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(containView);
        make.height.equalTo(@202);
    }];
    
    // 添加广本介绍
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.numberOfLines = 0;
    textLabel.font = [UIFont systemFontOfSize:15];
    textLabel.textColor = [UIColor yc_colorWithUInt:0x666666];
    [containView addSubview:textLabel];
    self.textLabel = textLabel;
    
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cycleScrollView.mas_bottom).offset(28);
        make.left.equalTo(containView).offset(21);
        make.right.equalTo(containView).offset(-21);
    }];
    
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(textLabel.mas_bottom).offset(28);
    }];
    
}

// 设置文字内容
- (void)setText:(NSString *)text
{
    if (!text) {
        text = @"";
    }
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    style.firstLineHeadIndent = 26;//首行缩进
    style.lineSpacing=11;//行距
    style.alignment=NSTextAlignmentLeft;
    
    NSAttributedString *textValue = [[NSAttributedString alloc]initWithString:text attributes:@{NSParagraphStyleAttributeName:style}];
    
    self.textLabel.attributedText= textValue;

}

@end
